<?php
// +----------------------------------------------------------------------
// | YotakuCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.yotaku.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Creatint <creatint@163.com> 
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取基础列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}



/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_nav_url($url){
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;        
        default:
            $url = U($url);
            break;
    }
    return $url;
}


/**
 * 获取用户组id
 * @param  int $uid    用户uid
 * @return int         用户组id
 * @author creatint <creatint@163.com>
 */
function get_group($uid){
    if($group = M('AuthGroupAccess')->where("`uid`={$uid}")->find()){
        return $group['group_id'];
    }
    return false;
}


/**
 * 获取用户所在大区
 * @param  string $username    用户名
 * @return string              所在大区
 * @author creatint <creatint@163.com>
 */
function get_dealerarea($username){
    $dealerarea = D('UcenterMember')->field('dealerarea')->where("`username`={$username}")->find();
    return $dealerarea['dealerarea'];
}


function add_search($query, $params){
    // 0:可以为0，1:直接搜索，2:模糊搜索
    $map = [
        'dealer_name'=>1,
        'plate_number'=>2,
        'status'=>0,
        'username'=>2,
        'city'=>2
    ];
    if ( $params && is_array($params) ) {
        foreach( $params as $k=>$v ){
            $v = preg_replace('/\s*/','',$v);
            if ( isset($map[$k]) && 1 == $map[$k] && $v ) {
                $query[$k] = $v;
            } elseif( isset($map[$k]) && 2 == $map[$k] && $v ) {
                $query[$k] = ['like',"%".trim($v," \n\r")."%"];
            } elseif( isset($map[$k]) && 0 == $map[$k] ) {
                $query[$k] = trim($v," \n\r");
            }
        }
    }
    return $query;
}